package com.feihua.starter.service.impl;

import com.feihua.starter.service.api.ApiHdbImagePoService;
import com.feihua.starter.service.mapper.HdbImagePoMapper;
import com.feihua.starter.service.modules.hdb.dto.HdbImageDto;
import com.feihua.starter.service.modules.hdb.dto.SearchHdbImagesConditionDto;
import com.feihua.starter.service.modules.hdb.po.HdbImagePo;
import com.github.pagehelper.Page;
import feihua.jdbc.api.pojo.PageResultDto;
import feihua.jdbc.api.service.impl.ApiBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * This class was generated by MyBatis Generator.
 * @author revolver 2018-06-30 17:54:42
 */
@Service
public class ApiHdbImagePoServiceImpl extends ApiBaseServiceImpl<HdbImagePo, HdbImageDto, String> implements ApiHdbImagePoService {
    @Autowired
    HdbImagePoMapper hdbImagePoMapper;

    public ApiHdbImagePoServiceImpl() {
        super(HdbImageDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpdate(List<HdbImageDto> hdbImageDtos, String relId, String userId) throws Exception {
        if (hdbImageDtos == null || hdbImageDtos.size() == 0) return;
        for (int i = 0; i < hdbImageDtos.size(); i++) {
            if (hdbImageDtos.get(i).getId() != null) {
                HdbImagePo hdbImagePo = new HdbImagePo();
                BeanUtils.copyProperties(hdbImageDtos.get(i), hdbImagePo);
                hdbImagePo.setRelId(relId);
                hdbImagePo.setSequence(i);
                super.preUpdate(hdbImagePo, userId);
                super.updateByPrimaryKeySelective(hdbImagePo);
                hdbImageDtos.get(i).setId(hdbImagePo.getId());
                hdbImageDtos.get(i).setRelId(relId);
                hdbImageDtos.get(i).setSequence(i);
            } else {
                HdbImagePo hdbImagePo = new HdbImagePo();
                BeanUtils.copyProperties(hdbImageDtos.get(i), hdbImagePo);
                hdbImagePo.setRelId(relId);
                hdbImagePo.setSequence(i);
                super.preInsert(hdbImagePo, userId);
                hdbImagePo = super.insertSelectiveSimple(hdbImagePo);
                hdbImageDtos.get(i).setId(hdbImagePo.getId());
                hdbImageDtos.get(i).setRelId(relId);
                hdbImageDtos.get(i).setSequence(i);
            }
        }
    }

    @Override
    public PageResultDto<HdbImageDto> searchHdbImagesDsf(SearchHdbImagesConditionDto dto, feihua.jdbc.api.pojo.PageAndOrderbyParamDto pageAndOrderbyParamDto) {
        Page p = super.pageAndOrderbyStart(pageAndOrderbyParamDto);
        List<HdbImageDto> list = this.wrapDtos(hdbImagePoMapper.searchHdbImages(dto));
        return new PageResultDto(list, this.wrapPage(p));
    }
}